package com.zl.learning.io.bio;

import java.io.IOException;
import java.net.*;

public class UDPServer {
    public static class Server1{
        public static void main(String[] args) {
            try {
                DatagramSocket socket = new DatagramSocket(999);
                while (true) {
                    byte[] bytes = new byte[1024];
                    DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                    socket.receive(dp);
                    String msg = new String(dp.getData(),0,dp.getLength());
                    System.out.println("收到："+msg);
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Server2{
        public static void main(String[] args) {
            try {
                MulticastSocket socket = new MulticastSocket(999);
                socket.joinGroup(InetAddress.getByName("224.0.0.5"));
                while (true) {
                    byte[] bytes = new byte[1024];
                    DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                    socket.receive(dp);
                    String msg = new String(dp.getData(),0,dp.getLength());
                    System.out.println("收到："+msg);
                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
